﻿@using Microsoft.AspNetCore.Components.Web.Virtualization
@typeparam TItem
@typeparam TValue
<Dropdown @ref="@dropdownRef" ElementId="@ElementId" Class="@Class" Style="@Style" RightAligned="@RightAligned" Disabled="@Disabled" Direction="@Direction" Attributes="@Attributes">
    <DropdownToggle @ref="@dropdownToggleRef" Color="@Color" Size="@DropdownToggleSize" TabIndex="@TabIndex">@ChildContent</DropdownToggle>
    <DropdownMenu MaxMenuHeight="@MaxMenuHeight">
        @if ( Data != null )
        {
            @if ( Virtualize && Data is ICollection<TItem> collectionableData )
            {
                <Virtualize TItem="TItem" Context="item" Items="@collectionableData">
                    @itemFragment( item )
                </Virtualize>
            }
            else
            {
                @foreach ( var item in Data ?? Enumerable.Empty<TItem>() )
                {
                    @itemFragment( item )
                }
            }
        }
    </DropdownMenu>
</Dropdown>
@code {
    protected RenderFragment<TItem> itemFragment => item => __builder =>
    {
        var text = GetItemText( item );
        var value = GetItemValue( item );
        var disabled = GetItemDisabled( item );

        <DropdownItem @key="@item" Clicked="@HandleDropdownItemClicked" Value="@value" Disabled="@disabled"
                      ShowCheckbox="@(SelectionMode == DropdownListSelectionMode.Checkbox)"
                      Checked="IsSelected(value)"
                      CheckedChanged="@((isChecked) => HandleDropdownItemChecked(isChecked, value))">@text</DropdownItem>
    };
}